<?php
/*******************************************************************************
 * $Date:: Jan 24, 2011 4:09:25 PM                                              $
 * $Revision:                                                                  $
 * $Author:: Tuan Duong                                                        $
 * $Comment::                                                                  $
 ******************************************************************************/ 
/**
 * Description here...
 * 
 * @package Lua
 * @subpackage ##Subpackage##
 * @author Tuan Duong <bacduong[at]gmail[dot]com>
 * @version 
 */

class Lua_Form_ElementDecorator extends Zend_Form_Decorator_Abstract
{
    private $errorsDecorator;

    public function __construct($options = null)
    {
        parent::__construct($options);
        $this->errorsDecorator = new Zend_Form_Decorator_Errors();
    }
    /**
     *
     * @param Zend_Form_Element $element
     * @param Zend_View $view
     * @return string
     */
    protected function buildLabel($element, $view)
    {
        $html = '<label for="';
        $html .= $view->escape($element->getId());
        $html .= '">';
        $html .= $view->escape($element->getLabel());
        if ($element->isRequired()) {
            $html .= '<abbr title="Required">*</abrr>';
        }
        $html .= '</label>';

        return $html;
    }

    /**
     * @param Zend_Form_Element $element
     * @param Zend_View $view
     * @return string
     */
    protected function buildInput($element, $view)
    {
        $attribs = $element->getAttribs();

        $stringLengthValidator = $element->getValidator('StringLength');
        if ($stringLengthValidator != null && $stringLengthValidator->getMax()) {
            $attribs['maxlength'] = $stringLengthValidator->getMax();
        }

        $helper  = $element->helper;
        return $view->$helper(
            $element->getFullyQualifiedName(),
            $element->getValue(),
            $attribs,
            $element->options
        );
    }

    /**
     * @param Zend_Form_Element $element
     * @param Zend_View $view
     * @return string
     */
    protected function buildErrors($element, $view)
    {
        $errorMessages = $element->getMessages();
        $errorMessage = null;
        if (count($errorMessages) > 0) {
            reset($errorMessages);
            $errorMessage = $this->getErrorMessage($element->getLabel(), key($errorMessages));
            if ($errorMessage == null) {
                $errorMessage = current($errorMessages);
            }
        } else {
            $errorMessages = $element->getErrorMessages();
            if (count($errorMessages) > 0) {
                $errorMessage = reset($errorMessages);
            }
        }

        if ($errorMessage) {
            return '<span class="error">' . $view->escape($errorMessage) . '</span>';
        } else {
            return '';
        }
    }

    protected function getErrorMessage($name, $errorCode)
    {
        switch ((string)$errorCode) {
            case 'isEmpty':
                return "$name is required";
            case 'regexNotMatch':
                return "$name is invalid";
            default:
                return null;
        }
    }

    public function render($content)
    {
        $element = $this->getElement();
        $view = $element->getView();

//        $label = $this->buildLabel($element, $view);
//        $this->errorsDecorator->setElement($element);

        $html = '<div class="field">';
        $html .= '<div class="field-label">' . $this->buildLabel($element, $view) . '</div>';
        $html .= '<div class="field-control">' . $this->buildInput($element, $view) . '</div>';
        $html .= '<div class="field-errors">' . $this->buildErrors($element, $view) . '</div>';
        $html .= '</div>';
//        $html = '<tr class="element">';
//        $html .= '<td>' . $label . '</td>';
//        $html .= '<td>' . $this->buildInput() . '</td>';
//        $html .= '<td>' . $this->errorsDecorator->render('') . '</td>';
//        $html .= '</tr>';
        return $content . $html;
    }
}

